package com.link.base.base.accnt.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.link.base.base.accnt.dao.mybatis.mapper.AcctChangeMapper;
import com.link.base.base.accnt.model.Account;
import com.link.base.base.accnt.model.AcctChange;
import com.link.base.base.approval.model.Flow;
import com.link.base.base.approval.service.FlowService;
import com.link.base.base.esb.service.LinkBaseDataApiService;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 客户变更
 *
 * @author 阳晓琳
 * @date 2020-10-12 14:25:07
 */
@Service
public class AcctChangeServiceImpl extends BasicServiceImpl<AcctChange> implements AcctChangeService {

    @Resource
    private AcctChangeMapper    acctChangeMapper;

    @Resource
    private AccountService accountService;

    @Resource
    private FlowService flowService;

    @Resource
    private LinkBaseDataApiService linkBaseDataApiService;

    @Override
    public BasicMapper<AcctChange> getBasicMapper() {
        return acctChangeMapper;
    }

    public void acctChangeSubmit(AcctChange acctChange) throws Exception {
        //获取变更信息
        AcctChange record = acctChangeMapper.queryById(acctChange);
        if (record == null) {
            throw new ServiceException("ACCTCHANGE-001");
        }

        //获取客户信息
        Account accountProper = new Account();
        accountProper.setId(record.getHeadId());
        Account account = accountService.queryById(accountProper);
        if (account == null) {
            throw new ServiceException("ACCTCHANGE-002");
        }

        String approvalType = "ChangeAccount";
        ObjectMapper mapper = new ObjectMapper();
        String approvalDetail = mapper.writeValueAsString(acctChange);
        //创建审批流程
        flowService.createFlow(acctChange.getId(), approvalType, null, acctChange.getAcctName(), acctChange.getId(), approvalDetail);
        acctChange.setAuditStatus("Approving");
        acctChangeMapper.auditUpdate(acctChange);
        account.setAuditStatus("Approving");
        accountService.auditUpdate(account);
    }

    @Override
    public void beforUpsert(AcctChange entity) throws Exception {
        Long acctId = entity.getHeadId();
        String acctCode = StringUtils.isBlank(entity.getAcctCode()) ? "" : entity.getAcctCode();
        String acctName = StringUtils.isBlank(entity.getAcctName()) ? "" : entity.getAcctName();
        String taxpayerNumber = StringUtils.isBlank(entity.getTaxpayerNumber()) ? "" : entity.getTaxpayerNumber();
        Account account = new Account();
        account.setAcctCode(acctCode);
        account.setAcctName(acctName);
        account.setTaxpayerNumber(taxpayerNumber);
        //校验是否有 客户编号 客户名称 纳税登记号相同的数据
        List<Account> accountList = accountService.queryAccountListByAcctChange(account);
        if (accountList.size() > 0) {
            for (Account record : accountList) {
                if (acctCode.equals(record.getAcctCode()) && acctId != record.getId()) {
                    throw new ServiceException("ACCTCHANGE-003");
                }
                if (acctName.equals(record.getAcctName()) && acctId != record.getId()) {
                    throw new ServiceException("ACCTCHANGE-004");
                }
                if (taxpayerNumber.equals(record.getTaxpayerNumber()) && acctId != record.getId()) {
                    throw new ServiceException("ACCTCHANGE-005");
                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void apprChangeAccount(Flow flow, String arg1, String arg2, String arg3, String arg4, String arg5) throws Exception {
        Long acctChangeId = flow.getFlowObjId();
        AcctChange acctChange = queryById(acctChangeId);
        if (acctChange == null) {
            throw new ServiceException("ACCTCHANGE-001");
        }
        acctChange.setAuditStatus(arg1);
        acctChangeMapper.auditUpdate(acctChange);
        Account accountProper = new Account();
        accountProper.setId(acctChange.getHeadId());
        Account account = accountService.queryById(accountProper);
        if (account == null) {
            throw new ServiceException("ACCTCHANGE-002");
        }
        if ("Approved".equals(arg1)) {
            accountService.accountChangeAppr(acctChange);

            //20210729 客户变更审批通过，同步数据至ERP
            linkBaseDataApiService.syncAccountChange(account);
        } else if ("Rejected".equals(arg1) || "New".equals(arg1)) {
            //20210729 审批驳回和撤回后，已在前面更新审批状态,无需其他操作
//            AcctChange changeRecord = new AcctChange();
//            changeRecord.setHeadId(acctChangeId);
//            changeRecord.setAuditStatus(arg1);
//            accountService.accountChangeAppr(acctChange);
        } else {
            throw new ServiceException("ACCTCHANGE-002");
        }
    }

    @Override
    public List<AcctChange> queryChangeInfo(AcctChange entity) throws Exception {
        AcctChange acctChange = queryById(entity);
        if (acctChange == null) {
            throw new ServiceException("ACCTCHANGE-001");
        }
        Account accountProper = new Account();
        accountProper.setId(acctChange.getHeadId());
        Account account = accountService.queryById(accountProper);
        if (account == null) {
            throw new ServiceException("ACCTCHANGE-002");
        }
        List<AcctChange> list = new ArrayList<>(2);

        AcctChange oldAccount = new AcctChange();
        oldAccount.setType("变更前");
        oldAccount.setAcctCode(account.getAcctCode());
        oldAccount.setAcctName(account.getAcctName());
        oldAccount.setAcctParentName(account.getAcctParentName());
        oldAccount.setTaxpayerNumber(account.getTaxpayerNumber());
        oldAccount.setAccountBankName(account.getAccountBankName());
        oldAccount.setRegTaxNum(account.getRegTaxNum());
        oldAccount.setBillAddr(account.getBillAddr());
        list.add(oldAccount);
        acctChange.setType("变更后");
        list.add(acctChange);
        return list;
    }
}